package com.lyc.controller;

import com.github.pagehelper.PageInfo;
import com.lyc.pojo.Address;
import com.lyc.service.AddressService;
import com.lyc.tool.Msg1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Controller
@RequestMapping("/addr")
@CrossOrigin
public class AddressController {
    @Autowired
    private AddressService addressService;

    /*当前用户新增收货地址*/
    @RequestMapping(value = "/addAddress",method = RequestMethod.POST)
    @ResponseBody
    public List<Address> addAddress(Address address){
        try {
            address.setName(new String(address.getName().getBytes("iso-8859-1"),"utf-8"));
            address.setShengshi(new String(address.getShengshi().getBytes("iso-8859-1"),"utf-8"));
            address.setCity(new String(address.getCity().getBytes("iso-8859-1"),"utf-8"));
            address.setQuxian(new String(address.getQuxian().getBytes("iso-8859-1"),"utf-8"));
            address.setAddrInfo(new String(address.getAddrInfo().getBytes("iso-8859-1"),"utf-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        List<Address> list= addressService.addAddress(address);
       return list;
    }
    /*当前用户更新某个地址*/
   @RequestMapping(value = "/updateAddress",method = RequestMethod.POST)
    @ResponseBody
    public List<Address> updateAddress(Address address) {
       try {
           address.setName(new String(address.getName().getBytes("iso-8859-1"),"utf-8"));
           address.setShengshi(new String(address.getShengshi().getBytes("iso-8859-1"),"utf-8"));
           address.setCity(new String(address.getCity().getBytes("iso-8859-1"),"utf-8"));
           address.setQuxian(new String(address.getQuxian().getBytes("iso-8859-1"),"utf-8"));
           address.setAddrInfo(new String(address.getAddrInfo().getBytes("iso-8859-1"),"utf-8"));
       } catch (UnsupportedEncodingException e) {
           e.printStackTrace();
       }
        List<Address> list = addressService.updateAddress(address);
        return list;
    }
    /*当前用户删除指定地址*/
    @RequestMapping(value = "/deleteAddress/{aid}/{uid}",method = RequestMethod.POST)
    @ResponseBody
    public List<Address> deleteAddress(@PathVariable("aid") Integer aid,@PathVariable("uid") Integer uid) {
        List<Address> list = addressService.deleteAddress(aid,uid);
        return list;
    }
    /*查询当前用户所有地址*/
    @RequestMapping(value = "/selectAllAddress/{uid}",method = RequestMethod.GET)
    @ResponseBody
    public List<Address> selectAllAddress(@PathVariable("uid") Integer uid){
        List<Address> list = addressService.selectAddress(uid);
        return list;
    }

    /*查询指定地址*/
    @RequestMapping(value = "/selectOneAddress/{aid}",method = RequestMethod.GET)
    @ResponseBody
    public Address selectone(@PathVariable("aid") Integer aid){
        Address address = addressService.selectOne(aid);
        return address;
    }

    /*后台代码*/
    @RequestMapping("/all")
    @ResponseBody

    public Msg1<Address> selectAll(@RequestParam(defaultValue = "1")Integer page,
                                   @RequestParam(defaultValue = "5")Integer limit){
        List<Address> addresses = addressService.selectAll();
        PageInfo<Address> pageInfo=addressService.getAddressByPage(page,limit);
        // System.out.println(pageInfo);
        Msg1<Address> msg=new Msg1<>();
        //count属性类型改为long值
        msg.setCount(pageInfo.getTotal());
        msg.setCode(0);
        msg.setData(pageInfo.getList());
        return msg;
    }
}
